package com.coocaa.internship.util;

import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

/**
 * @Description: BindingResult工具类
 * @Author: zwh
 * @Date: 2019-07-28
 * @Time: 12:43
 */
@Slf4j
public class BindingResultUtil {
    /**
     * 处理绑定参数错误
     */
    public static void resolveErrors(BindingResult bindingResult) {
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        String message = "";
        for (ObjectError error : allErrors) {
            message += error.getDefaultMessage() + " ";
            log.info(error.getCode() + "------------>" + error.getDefaultMessage());
        }
        throw new BaseException(ResultCode.PARAMETER_ERROR.getCode(), message);
    }
}
